﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.Web;
using SCMS.Model;
using SCMS.Reports;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.CompletionCtificate;
using SCMS.UI.Models;

namespace SCMS.UI.Areas.Reports.Controllers
{
    [MyException]
    public class CompletionCController : PortalBaseController
    {
        private ICompletionCertificateService ccService;
        public CompletionCController(IPermissionService permissionService, IUserContext userContext, ICompletionCertificateService ccService)
            : base(userContext, permissionService)
        {
            this.ccService = ccService;
        }

        //
        // GET: /Reports/CompletionC/

        public ActionResult Index(Guid CCid)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            Models.ViewCC modelCC = CompletionCExtention.PrepareCC(CCid, ccService);
            using (var db = new SCMSEntities())
            {
                data.Add("{DATE}", modelCC.EntityCC.PreparedOn.Value.ToString("dd/MM/yyyy"));
                data.Add("{RegNo}", modelCC.EntityCC.RefNumber);
                data.Add("{OFFICE}", modelCC.EntityCC.CountrySubOffice.Name);
                data.Add("{COUNTRY}", modelCC.EntityCC.CountrySubOffice.CountryProgramme.Country.Name);
                data.Add("{PREPAREDBY}", modelCC.PreparedBy.StaffName);
                data.Add("{TITLE}", modelCC.PreparedBy.Designation);
                if (modelCC.PreparedBy != null && modelCC.PreparedBy.SignatureImage != null)
                    data.Add("{SIGNATURE}", "data:image/png;base64," + Convert.ToBase64String(modelCC.PreparedBy.SignatureImage));
                data.Add("{PURPOSE}", modelCC.EntityCC.Purpose);

                data.Add("{CONTRUCTOR}", modelCC.EntityCC.PurchaseOrder.Supplier.Name);
                data.Add("{PONO}", modelCC.EntityCC.PurchaseOrder.RefNumber);
                data.Add("{PROJECTTITLE}", modelCC.EntityCC.ProjectTitle);
                data.Add("{LOCATION}", modelCC.EntityCC.Location);
                data.Add("{DRCOFFICE}", modelCC.EntityCC.CountrySubOffice1.Name);
                data.Add("{COMFIRMED}", modelCC.ComfirmedBy.StaffName);

                data.Add("{COMPLETE}", modelCC.EntityCC.WorksService == "Completed satisfactory" ? "\" checked=\"checked\"" : "");
                data.Add("{OUTSTANDING}", modelCC.EntityCC.WorksService == "Outstanding issues" ? "\" checked=\"checked\"" : "");
                data.Add("{NOTCOMPLETED}", modelCC.EntityCC.WorksService == "Not Completed" ? "\" checked=\"checked\"" : "");

                data.Add("{REMARKS}", modelCC.EntityCC.Remarks);

               
                data.Add("{APPROVTITLE}", (modelCC.AprovedBy != null)?modelCC.AprovedBy.Designation:"");
                data.Add("{APPROVERNAME}", (modelCC.AprovedBy != null) ? modelCC.AprovedBy.StaffName : "");
                data.Add("{APPROVERSIGNATURE}", (modelCC.AprovedBy != null && modelCC.AprovedBy.SignatureImage != null) ? "data:image/png;base64," + Convert.ToBase64String(modelCC.AprovedBy.SignatureImage) : "");
            }

            List<String> options = new List<string>();
            options.Add(" --copies 2 ");
            Byte[] output = WkHtml2Pdf.CreateReport(data, "CompletionCertificate.htm", options);

            return File(output, "application/pdf", "CC_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }

    }
}
